import plugin from '../../../lib/plugins/plugin.js'  
import { segment } from 'oicq';  
import fetch from 'node-fetch';  
  
export class RealTimeBusQuery extends plugin {  
  constructor() {  
    super({  
      name: '实时公交查询',  
      dsc: '查询实时公交到站信息',  
      event: 'message',  
      priority: 10,  
      rule: [  
        {  
          reg: /^查询实时公交(.*)到(.*)$/,  
          fnc: 'queryRealTimeBus'  
        }  
      ]  
    });  
  }  
  
  async queryRealTimeBus(e) {  
    const [, city, line] = e.msg.match(/^查询实时公交(.*)到(.*)$/);  
    if (!city || !line) {  
      return await e.reply('请按照“查询实时公交 城市 公交线路”的格式输入');  
    }  
    
    const url = `https://api.lolimi.cn/API/che/api.php?type=text&city=${encodeURIComponent(city)}&line=${encodeURIComponent(line)}`;  
    try {  
      const res = await fetch(url);  
      if (!res.ok) {  
        throw new Error('网络响应错误: ' + res.statusText);  
      }  
      const data = await res.text();  
      
      // 跳过标题行  
      const lines = data.split('\n').slice(2); // 假设前两行是标题和分隔行  
      const busInfo = lines.join('\n'); // 将剩余的行合并回一个字符串  
      
      await e.reply(busInfo.trim()); // 发送处理后的公交信息  
    } catch (error) {  
      console.error('查询实时公交失败', error);  
      await e.reply('查询实时公交失败，请稍后再试');  
    }  
  }
}